//
//  MRMCommodityViewController.m
//  MeiRiMoney
//
//  Created by qiuqk on 13-6-23.
//  Copyright (c) 2013年 MeiRiCai. All rights reserved.
//

#import "MRMCommodityViewController.h"
#import "MRMSdk.h"
#import "MRMCommodityTableCell.h"
#import "MRMAlipayWithdrawViewController.h"
#import "MRMMobileRechargeViewController.h"
#import "MRMUtils.h"
#import "MRMData.h"

@interface MRMCommodityViewController ()
{
	NSArray *commodities;
}


@end

@implementation MRMCommodityViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	[MRMUtils setBackButton:self];
	[MRMUtils setTopBarTitle:self title:self.navigationItem.title];
	self.tableView.allowsSelection = NO;
	
	commodities = [MRMData getCommodities];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)goBack
{
	[self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return commodities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MRMCommodityTableCell";
    MRMCommodityTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil)
	{
		UINib *nib = [UINib nibWithNibName:@"MRMCommodityTableCell" bundle:nil];
		NSArray *array = [nib instantiateWithOwner:self options:nil];
		
		cell = array.lastObject;
	}
	MRMCommodity *commodity = [commodities objectAtIndex:indexPath.row];
	cell.commodity = commodity;
	cell.navigationController = self.navigationController;
	cell.commodityImageView.image = [UIImage imageNamed:commodity.ImageName];
	cell.commodityNameLabel.text = commodity.Name;
	cell.commodityIntegralLabel.text = [MR_Convert longToString:commodity.Integral];
	
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	MRMCommodity *commodity = [commodities objectAtIndex:indexPath.row];
	
	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"通知" message:@"积分不够，请做任务" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
	[alertView show];
	return;
	
	UIViewController *viewController = nil;
	if (commodity.Category == 1)
	{
		MRMMobileRechargeViewController *mobileView = [[MRMMobileRechargeViewController alloc] initWithNibName:@"MRMMobileRechargeViewController" bundle:nil];
		
		viewController = mobileView;
		viewController.title = @"手机充值";
	}
	else if (commodity.Category == 2)
	{
		MRMAlipayWithdrawViewController *alipayView = [[MRMAlipayWithdrawViewController alloc] initWithNibName:@"MRMAlipayWithdrawViewController" bundle:nil];
		alipayView.commodity = commodity;
		viewController = alipayView;
		viewController.title = @"提现到支付宝";
	}

	[self.navigationController pushViewController:viewController animated:YES];
}

@end
